package lily.phs.urh;

import lily.phs.object.HereTipa;
import lily.phs.object.JieZhenRecord;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;
import lily.ui.control.SquareItemAdapter;



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class JiezhenView extends RelativeLayout implements OnClickListener{
	
	
	private LinearLayout ll1;
	private EditText et_name,et_id,
					 et_zhuguan,et_keguan,et_pinggu,et_plan,
					 et_doctor,et_date;
	
	private Context myContext;
	private int profileid,state;
	MyPopupWindow mPopupWindow;
	
	HereTipa ht;
	JieZhenRecord jrc;
	View v;
	
	public JiezhenView(Context context,int pro,int state) {
		super(context);
		myContext=context;
		this.profileid=pro;
		this.state=state;
	}
	
	public void initWeidget(){
		LayoutInflater factory = LayoutInflater.from(myContext);
        v = factory.inflate(R.layout.jiezhen_record, null);
        ll1=(LinearLayout)v.findViewById(R.id.ll);
		et_name=(EditText)v.findViewById(R.id.et_jiezhen_name);
		et_id=(EditText)v.findViewById(R.id.et_jiezhen_id);
		et_name.setInputType(InputType.TYPE_NULL);
		et_id.setInputType(InputType.TYPE_NULL);
		et_zhuguan=(EditText)v.findViewById(R.id.et_jiezhen_zhuguan);
		et_keguan=(EditText)v.findViewById(R.id.et_jiezhen_keguan);
		et_pinggu=(EditText)v.findViewById(R.id.et_jiezhen_pinggu);
		et_plan=(EditText)v.findViewById(R.id.et_jiezhen_plan);
		et_doctor=(EditText)v.findViewById(R.id.et_jiezhen_doctor);
		et_date=(EditText)v.findViewById(R.id.et_jiezhen_date);
		et_date.setInputType(InputType.TYPE_NULL);
		et_date.setOnClickListener(this);
		
		ht=new HereTipa(myContext);
		ht.selectNameAndCode(profileid);
		jrc=new JieZhenRecord(myContext);
		jrc.selectJieZhenRecord(profileid, state);
	}
	
	public void initView(){
		LayoutParams lp=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		this.addView(v,lp); 
		MyView vv=new MyView(myContext);
        this.addView(vv, lp);
		et_id.setText(ht.getHeti_here_id());
		et_name.setText(ht.getHeti_name());
		et_zhuguan.setText(jrc.getThre_obje_info());
		et_keguan.setText(jrc.getThre_subj_info());
		et_pinggu.setText(jrc.getThre_eval());
		et_plan.setText(jrc.getThre_plan());
		et_doctor.setText(jrc.getThre_doct());
		et_date.setText(jrc.getThre_date());
	}
	
	public void selectData(int state){
		jrc.selectJieZhenRecord(profileid, state);
	}
	
	public void initView2(){
		et_id.setText(ht.getHeti_here_id());
		et_name.setText(ht.getHeti_name());
		et_zhuguan.setText(jrc.getThre_obje_info());
		et_keguan.setText(jrc.getThre_subj_info());
		et_pinggu.setText(jrc.getThre_eval());
		et_plan.setText(jrc.getThre_plan());
		et_doctor.setText(jrc.getThre_doct());
		et_date.setText(jrc.getThre_date());
	}
	
	public void saveView(){
		jrc.setThre_obje_info(et_zhuguan.getText().toString());
		jrc.setThre_subj_info(et_keguan.getText().toString());
		jrc.setThre_eval(et_pinggu.getText().toString());
		jrc.setThre_plan(et_plan.getText().toString());
		jrc.setThre_doct(et_doctor.getText().toString());
		jrc.setThre_date(et_date.getText().toString());
		jrc.updateJieZhenRecord(profileid, state);
	}
	
	class MyView extends View {
    	View v;
    	Paint paint;
    	
		public MyView(Context context) {
			super(context);
			paint=new Paint();
			paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
		}
		
		@Override
		public void onDraw(Canvas c){
	        int top=et_name.getBottom();
	        int left=ll1.getLeft();
	        int right=ll1.getRight();
	        int bottom=ll1.getBottom();
	        int y=0;//sv.getScrollY();
	        top=top-y;
	        bottom=bottom-y;
	        
	        c.drawLine(left, top+130, right, top+130, paint);
	        c.drawLine(left, top+131, right, top+131, paint);
	        c.drawLine(left, bottom, right, bottom, paint);
	        c.drawLine(left, bottom+1, right, bottom+1, paint);
		}
    	
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_date){
			mPopupWindow = new MyPopupWindow(myContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.et_jiezhen_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
	}

	
}


